package com.baiyang.app.ui.mine;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.MyApp;
import com.baiyang.app.R;
import com.baiyang.app.databinding.FragmentClassificationBinding;
import com.baiyang.app.databinding.FragmentMineBinding;
import com.baiyang.app.model.Article;
import com.baiyang.app.model.Goods;
import com.baiyang.app.model.User;
import com.baiyang.app.tool.AlertUtil;
import com.baiyang.app.tool.DataTool;
import com.baiyang.app.ui.classification.ClassificationService;
import com.baiyang.app.ui.classification.ClassificationViewModel;
import com.baiyang.app.ui.mine.commentRecord.RecordCommentActivity;
import com.baiyang.app.ui.mine.exchange.RecordExchangeActivity;
import com.baiyang.app.ui.mine.exchange.RecordExchangeViewModel;
import com.baiyang.app.ui.mine.goods.GoodsActivity;
import com.baiyang.app.ui.mine.readRecord.RecordReadActivity;
import com.baiyang.app.ui.mine.spendRecord.RecordSpendActivity;

/**
 */
public class MineFragment extends Fragment {

    private FragmentMineBinding binding;
    private MineViewModel viewModel;
    private MineService service;
    private User user;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMineBinding.inflate(inflater, container, false);
        viewModel = new ViewModelProvider(this).get(MineViewModel.class);
        service = new MineService(getContext(),viewModel);
        View root = binding.getRoot();
        init();
        initHandler();
        return root;
    }

    private void initHandler() {
        viewModel.getBookCoin().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.bookCoin.setText(s);
            }
        });
        viewModel.getIntegral().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.integral.setText(s);
            }
        });
        viewModel.getSignInResult().observe(getViewLifecycleOwner(), new Observer<JSONObject>() {
            @Override
            public void onChanged(JSONObject jsonObject) {
                String result = jsonObject.getString("result");
                if("SUCCESS".equals(result)){
                    JSONObject object = jsonObject.getJSONObject("data");
                    String s = object.getString("integralNum");
                    binding.integral.setText(s);
                }else{
                    String detail = jsonObject.getString("detail");
                    Toast.makeText(getContext(), detail, Toast.LENGTH_SHORT).show();
                }
            }
        });
        viewModel.getRechargeResult().observe(getViewLifecycleOwner(), new Observer<JSONObject>() {
            @Override
            public void onChanged(JSONObject jsonObject) {
                String result = jsonObject.getString("result");
                if("SUCCESS".equals(result)){
                    JSONObject object = jsonObject.getJSONObject("data");
                    String s = object.getString("bookCoin");
                    binding.bookCoin.setText(s);
                }else{
                    String detail = jsonObject.getString("detail");
                    Toast.makeText(getContext(), detail, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init() {
        user = MyApp.getInstance().getUser();
        binding.userName.setText(user.getUserName());
        // 签到
        binding.signIn.setOnClickListener(e->{
            service.signIn(user);
        });
        // 充值
        binding.recharge.setOnClickListener(e->{
            AlertUtil alertUtil = new AlertUtil(getActivity(),R.layout.dialog_recharge,true) {
                @Override
                public void setView(View view) {

                    view.findViewById(R.id.define).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String money = ((EditText)view.findViewById(R.id.money)).getText().toString().trim();
                            if(DataTool.isBlank(money)){
                                Toast.makeText(getContext(),"请输入",Toast.LENGTH_SHORT).show();
                            }
                            service.recharge(money,user.getId());
                            dismiss();
                        }
                    });
                    view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                }
            };
            alertUtil.show();
        });
        // 查看阅读记录
        binding.readRecord.setOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getContext(), RecordReadActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.left_in,R.anim.right_out);
        });
        // 查看消费记录
        binding.spendRecord.setOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getContext(), RecordSpendActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.left_in,R.anim.right_out);
        });
        // 查看历史评价
        binding.commentRecord.setOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getContext(), RecordCommentActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.left_in,R.anim.right_out);
        });
        // 查看兑换商品信息
        binding.exchange.setOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getContext(), GoodsActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.left_in,R.anim.right_out);
        });
        // 查看兑换记录
        binding.exchangeRecord.setOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getContext(), RecordExchangeActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.left_in,R.anim.right_out);
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        service.getBookCoinAndIntegral(user);
    }
}